<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\common\controller;

use cmf\controller\RestBaseController;

class ApiBaseController extends RestBaseController
{

    /**
     *  获取 access_secret
     * @return void
     */
    protected function getAccessToken()
    {
        $accessToken = cache('access_secret');

        if (!empty($accessToken)) {
            return $accessToken;
        }
        $sendData = [
            'grant_type' => 'client_credential',
            'appid' => ENV('APPID'),
            'secret' => ENV('SECRET'),
        ];

        $sendData = http_build_query($sendData);
        $accessToken = cmf_curl_get('https://api.weixin.qq.com/cgi-bin/token' . '?' . $sendData);
        $accessToken = json_decode($accessToken,true); 

        if (!isset($accessToken['errcode'])) {
            cache('access_secret', $accessToken['access_token'], 7200);
            return $accessToken['access_token'];
        } else {
            $this->error($accessToken['errmsg']);
        }

    }

    protected function is_error($arr = [])
    {
        if (isset($arr['code']) && $arr['code'] == 0){
                return true;
        }
        return  false;
    }

}
